package cn.cnlee.app.ui.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import cn.cnlee.app.R;
import cn.cnlee.app.config.Constants;
import cn.cnlee.app.ui.news.NewsContentFragment;
import cn.cnlee.arch.base.BaseActivity;
import cn.cnlee.arch.base.BaseFragment;

public class CommonActivity extends BaseActivity implements View.OnClickListener{

    private String typeFragment;
    private Bundle bundle;
    private FragmentTransaction ft;
    private TextView barTitle;

    @Override
    protected void onStateRefresh() {

    }

    @Override
    protected void initStatusBar() {
        super.initStatusBar();
    }

    @Override
    public int getLayoutId() {
        return R.layout.common_activity_layout;
    }

    @Override
    public void initViews(Bundle savedInstanceState) {
        loadManager.showSuccess();
        ImageView barBack = findViewById(R.id.iv_back);
        barBack.setVisibility(View.VISIBLE);
        RelativeLayout mTitleLayout = findViewById(R.id.rl_title_bar);
        mTitleLayout.setVisibility(View.VISIBLE);
        barTitle = findViewById(R.id.tv_title);
        getIntentData();
        FragmentManager fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
//        ft.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_left_out);
        showFragment();
        barBack.setOnClickListener(this);
    }

    private void getIntentData() {
        Intent intent = getIntent();
        if (intent != null) {
            typeFragment = intent.getStringExtra("type_fragment");
            String titleName = intent.getStringExtra("title_name");
            bundle = intent.getBundleExtra("bundle_fragment");
            barTitle.setText(titleName);
        }

    }

    private void showFragment() {
        BaseFragment fragment = NewsContentFragment.newInstance();
        fragment.setArguments(bundle);
        switch (typeFragment) {
            case Constants.NEWS_CONTENT_TAG:
                commitFragment(fragment);
                break;
            default:
                break;
        }

    }

    public void commitFragment(BaseFragment baseFragment) {
        ft.replace(R.id.fragment_content, baseFragment).commit();
    }

    public static void start(Context context, String typeFragment, String titleName, Bundle bundle) {
        Intent starter = new Intent(context, CommonActivity.class);
        starter.putExtra("type_fragment", typeFragment);
        starter.putExtra("title_name", titleName);
        starter.putExtra("bundle_fragment", bundle);
        context.startActivity(starter);
    }

    @Override
    public void onClick(View v) {
        finish();
    }
}
